package com.example.jiradata.utils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

@Component("httpClientUtils")
public class HttpClientUtils {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    private static final String CONTENT_TYPE = "application/json; charset=UTF-8";

    private static final String ACCEPT_ENCODING = "gzip, deflate, br";

    private static final String ACCEPT_LANGUAGE = "zh-CN,zh;q=0.8,en;q=0.6";

    private static final String STR_ACCEPT = "Accept";
    private static final String STR_ACCEPT_ENCODING = "Accept-Encoding";
    private static final String STR_ACCEPT_LANGUAGE = "Accept-Language";

    @Autowired
    private RestTemplate restTemplate;



    public <T> T exchange(String url, ParameterizedTypeReference<T> responseType, Object requestBody, HttpMethod method) {
        try {

            HttpHeaders headers = new HttpHeaders();
           /* MediaType type = MediaType.parseMediaType(CONTENT_TYPE);
            headers.setContentType(type);
            headers.add(STR_ACCEPT, MediaType.APPLICATION_JSON.toString());
            headers.add(STR_ACCEPT_ENCODING, ACCEPT_ENCODING);
            headers.add(STR_ACCEPT_LANGUAGE, ACCEPT_LANGUAGE);*/
            headers.add("Authorization","Basic YWpheC5ndUBzaW5vZHluYW1pYy5jb206cFVWTnRPTFNOaDNEQ1FlTnlacWI2NzU0");
            HttpEntity<String> formEntity = null == requestBody ? new HttpEntity(headers) : new HttpEntity(requestBody, headers);

            return restTemplate.exchange(url, method, formEntity, responseType).getBody();
        }catch (RestClientException e){
            logger.error("url:{} prams:{} responseType:{} urlVariables:{}", url, requestBody, responseType, e);
            throw e;
        }
    }


}
